#include "cardReaderDev.h"
#include <winscard.h>

namespace SDHuaYi_Script
{
    /* ***************************************** */
    cardReaderDev::cardReaderDev()
    {
    }

    bool cardReaderDev::CardReset()
    {
        std::string ATR;
        int res = piReader->insert(ATR);
        Card.strATS = QString::fromStdString(ATR);

        // 获取UID
        SendAPDU("FFCA000000");
        if (m_response.count() >= 8)
            Card.strUID = m_response.mid(0, 8);
        m_response = Card.strATS;

        if (res == SCARD_S_SUCCESS)
            return true;
        return false;
    }

    bool cardReaderDev::SendAPDU(QString sendMessage)
    {
        std::string sendBuff = sendMessage.toStdString();
        std::string recvBuff;
        int ret = piReader->send_hex(sendBuff, recvBuff);

        if (ret == SCARD_S_SUCCESS)
        {
            m_response = QString::fromStdString(recvBuff);
            m_ss = m_response.mid(m_response.length() - 4, 4);
            if (m_response.length() > 4)
            {
                m_response = m_response.mid(0, m_response.length() - 4);
            }
            else
            {
                m_response.clear();
            }
            return true;
        }
        else
        {
            m_response.clear();
            m_ss.clear();
            return false;
        }
    }

    /* ********************************************* */
    HY_RSA_PUBLIC_KEY::HY_RSA_PUBLIC_KEY()
    {
    }

}
